# -*- coding: utf-8 -*-

'''
时间的处理
'''
'''
datetime是Python处理日期和时间的标准库。

'''
print '获取当前日期和时间'
from datetime import datetime
now = datetime.now() # 获取当前datetime
print now #2017-02-10 10:13:22.532000
print type(now)
print '获取指定日期和时间'
dt = datetime(2017, 4, 19, 12, 20) # 用指定日期时间创建datetime
print dt
print 'datetime转换为timestamp'

print 'timestamp转换为datetime'
t = 1429417200.0
print(datetime.fromtimestamp(t))
'''
timestamp是一个浮点数，它没有时区的概念，而datetime是有时区的
'''
print 'str转换为datetime'
print(now.strftime('%a, %b %d %H:%M'))
print 'datetime加减,使用timedelta你可以很容易地算出前几天和后几天的时刻'
from datetime import timedelta
print now + timedelta(hours=10)
print now - timedelta(days=1)
print now + timedelta(days=2, hours=12)

d = datetime.strptime('2015-08-28 16:43:37.283', "%Y-%m-%d %H:%M:%S.%f")
t = d.timetuple()
import time
timeStamp = int(time.mktime(t))
timeStamp = float(str(timeStamp) + str("%06d" % d.microsecond))/1000000
print timeStamp
'''
py中可能涉及的time有四种类型
    1. time string
    2. datetime tuple(datetime obj)
    3. time tuple(time obj)
    4. timestamp
'''
print '1、time string:string是最简单的表示time的方式'
print time.ctime() # 'Fri Feb 10 16:18:27 2017'
time_string = '2000-01-02 03:04:05'


